package com.tit.safetycontacts;

import java.util.ArrayList;
import java.util.List;

import com.tit.safetycontacts.R;
import com.tit.safetycontacts.R.drawable;
import com.tit.safetycontacts.R.layout;
import com.tit.safetycontacts.utils.TabHostActivity;
import com.tit.safetycontacts.utils.TabItem;
import com.tit.safetycontacts.view.Tab1Activity;
import com.tit.safetycontacts.view.Tab2Activity;
import com.tit.safetycontacts.view.Tab3Activity;
import com.tit.safetycontacts.view.Tab4Activity;
import com.tit.safetycontacts.view.Tab5Activity;
import com.tit.safetycontacts.view.contacts.HomeContactActivity;
import com.tit.safetycontacts.view.dial.HomeDialActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.TabWidget;
import android.widget.TextView;

/**
 * <p>
 * 整个流程就像使用ListView自定BaseAdapter一样
 * </p>
 * 
 * <p>
 * 如果要自定义TabHostActivity的Theme，并且不想要头部阴影 一定要添加这个android:windowContentOverlay =
 * null
 * </p>
 * 
 * */
public class TabMainActivity extends TabHostActivity {

	List<TabItem> mItems;
	private LayoutInflater mLayoutInflater;
	public TabWidget tabWidget;

	/**
	 * 在初始化TabWidget前调用 和TabWidget有关的必须在这里初始化
	 */
	@Override
	protected void prepare() {

		TabItem dial = new TabItem("拨号", // title
				R.drawable.tab_dial, // icon
				R.drawable.example_tab_item_bg, // background
				new Intent(this, HomeDialActivity.class)); // intent

		TabItem contacts = new TabItem("联系人", R.drawable.tab_buddy,
				R.drawable.example_tab_item_bg, new Intent(this,
						HomeContactActivity.class));

		TabItem message = new TabItem("信息", R.drawable.tab_dialog,
				R.drawable.example_tab_item_bg, new Intent(this,
						Tab3Activity.class));

		TabItem safetyCenter = new TabItem("安全中心", R.drawable.tab_settings,
				R.drawable.example_tab_item_bg, new Intent(this,
						Tab4Activity.class));

		// TabItem more = new TabItem("更多", R.drawable.icon_more,
		// R.drawable.example_tab_item_bg, new Intent(this,
		// Tab5Activity.class));

		mItems = new ArrayList<TabItem>();
		mItems.add(dial);
		mItems.add(contacts);
		mItems.add(message);
		mItems.add(safetyCenter);
		// mItems.add(more);

		// 设置分割线
		tabWidget = getTabWidget();
		tabWidget.setDividerDrawable(R.drawable.tab_divider);

		mLayoutInflater = getLayoutInflater();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setCurrentTab(0);
	}

	/** tab的title，icon，边距设定等等 */
	@Override
	protected void setTabItemTextView(TextView textView, int position) {
		textView.setPadding(3, 3, 3, 3);
		textView.setText(mItems.get(position).getTitle());
		textView.setBackgroundResource(mItems.get(position).getBg());
		textView.setCompoundDrawablesWithIntrinsicBounds(0, mItems
				.get(position).getIcon(), 0, 0);

	}

	/** tab唯一的id */
	@Override
	protected String getTabItemId(int position) {
		return mItems.get(position).getTitle(); // 我们使用title来作为id，你也可以自定
	}

	/** 点击tab时触发的事件 */
	@Override
	protected Intent getTabItemIntent(int position) {

		return mItems.get(position).getIntent();
	}

	@Override
	protected int getTabItemCount() {
		return mItems.size();
	}

	/** 自定义头部文件 */
	@Override
	protected View getTop() {
		return mLayoutInflater.inflate(R.layout.example_top, null);
	}

}
